/**
 * @param {number[][]} grid
 * @return {number}
 */
var minPathSum = function(grid) {
    let rL = grid.length, cl = grid[0].length
    for(let i = 0; i < rL; i++) {
        for(let j = 0; j < cl; j++) {
            if(i === 0 && j !== 0) {
                grid[i][j] += grid[i][j - 1]
            } else if(j === 0 && i !==0) {
                grid[i][j] += grid[i - 1][j]
            } else if(i !== 0 && j !== 0) {
                grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1])
            }
        }
    }
    return grid[rL-1][cl - 1];
};
console.log(minPathSum([[1,3,1],[1,5,1],[4,2,1]]));